﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CardTriggerListener :MonoBehaviour, IPointerDownHandler
{
    public delegate void CardOnPointDown(GameObject gameObject);
    public CardOnPointDown carOnPointDown { get; set; }
    [HideInInspector]
    public string mName { get; set; }
    [HideInInspector]
    public Action mAction { get; set; }

    //冷却时间
    public int mClodTime = 5;
    //价格
    public int mPrice = 100;
    //是否冷却
    private bool isCold=false;
    //当前冷却的时间
    private float mCurrentTime = 0;    

    private void Start()
    {
        mName = this.name;
        mPrice = int.Parse(this.transform.GetChild(0).GetComponent<Text>().text);
        mAction = delegate {
            OnCancelState();
            isCold = true;
            Text moneyText = GameObject.Find("Money").GetComponentInChildren<Text>();
            int monoyCount = int.Parse(moneyText.text);
            moneyText.text = (monoyCount - mPrice).ToString();
            };
    }


    public void OnPointerDown(PointerEventData eventData)
    {
        int money = int.Parse(GameObject.Find("Money").GetComponentInChildren<Text>().text);
        if (money >= mPrice)
        {
            if (carOnPointDown != null)
            {
                carOnPointDown(this.gameObject);
            }
        }        
    }

    private void OnCancelState()
    {
        //不能触发
        this.GetComponent<Image>().raycastTarget = false;
        this.GetComponent<Button>().interactable = false;

        //显示黑色的遮罩ui
        Color color = this.transform.Find("Mask").GetComponent<Image>().color;
        color.a = 0.5f;
        this.transform.Find("Mask").GetComponent<Image>().color = color;
        this.transform.Find("Mask").GetComponent<Image>().fillAmount = 1;

        //没有被选中
        this.transform.Find("direction").GetComponent<CanvasGroup>().alpha = 0;
    }

    private void OnActState()
    {
        //可以触发
        this.GetComponent<Image>().raycastTarget = true;
        this.GetComponent<Button>().interactable = true;

        //不显示黑色的遮罩ui
        Color color = this.transform.Find("Mask").GetComponent<Image>().color;
        color.a = 0f;
        this.transform.Find("Mask").GetComponent<Image>().color = color;
        this.transform.Find("Mask").GetComponent<Image>().fillAmount = 1;
    }

    private void Update()
    {
        int money = int.Parse(GameObject.Find("Money").GetComponentInChildren<Text>().text);

        if (isCold)
        {            
            //开始冷却
            mCurrentTime += Time.deltaTime;
            this.transform.Find("Mask").GetComponent<Image>().fillAmount = 1 - mCurrentTime / mClodTime;
            if (mCurrentTime >= mClodTime)
            {                
                isCold = false;
                mCurrentTime = 0;
            }
        }        
        
        if (!isCold && mPrice <= money)
        {
            //激活状态
            OnActState();
        }
        if (!isCold && mPrice > money)
        {
            OnCancelState();
        }
    }    
}
